#include "MyCanvas.h"

MyCanvas::MyCanvas(QWidget* Parent, const QPoint& Position, const QSize& Size, Carte* b, Camp* c, int idJ, int* e, Client* cl) :
    QSFMLCanvas(Parent, Position, Size), back(b), camps(c), idJoueur(idJ), etat(e), _client(cl)
{
}

void MyCanvas::setIdSelection(int x, int y)
{
    if (indiceSelection > -1)
    {
        back->territoires[indiceSelection]->setFillColor(false);
        back->territoires[indiceSelection]->setOutlineColor(false);
    }
    if (indiceSelection2 > -1)
    {
        back->territoires[indiceSelection2]->setFillColor(false);
        back->territoires[indiceSelection2]->setOutlineColor(false);
    }

    indiceSelection = x;
    indiceSelection2 = y;
}

sf::Sprite& MyCanvas::getS()
{
    return s;
}

sf::Sprite& MyCanvas::getD()
{
    return d;
}

void MyCanvas::onInit()
{
    indiceSelection = -1;
    indiceSelection2 = -1;

    alreadySend = false;

    back->creerGrilleSelection();

    back->updateTexture(s, d);

    tF.loadFromFile(QApplication::applicationDirPath().toStdString() + "/Fin.png");
    sF.setTexture(tF);
    sF.setPosition(20, 20);

    clock.restart();
}

void MyCanvas::onUpdate()
{
    clear(sf::Color::White);

    if (! sf::Mouse::isButtonPressed(sf::Mouse::Left))
    {
        alreadySend = false;
    }

    if (!alreadySend && (((*etat == eCOMBAT && sf::Mouse::isButtonPressed(sf::Mouse::Left) ) && isIn()) && (indiceSelection == -1 || indiceSelection2 == -1)) )
    {
        alreadySend = true;
        if (isInFinTour())
        {
            sf::Packet p;
            p << eGAME_ETAT;
            _client->send(p, false);
        }
        else
        {
            if (indiceSelection > -1) // si un territoire a deja ete selectionne
            {
                sf::Int8 x;
                bool estSelect = false;

                back->selectionnerTerritoire(sf::Mouse::getPosition().x - mapToGlobal(QPoint(0, 0)).x(), sf::Mouse::getPosition().y - mapToGlobal(QPoint(0, 0)).y(), indiceSelection2);
                if (indiceSelection2 == indiceSelection) // si c'est le meme qu'auparavant
                {
                    // on le deselectionne
                    x = -1;
                    estSelect = true;
                }
                else if (indiceSelection2 > -1 && back->territoires[indiceSelection]->attaquable(back->territoires[indiceSelection2])) // si c'est un territoire que l'on peut attaquer (adjacent et appartient a un camp different)
                {
                    // on le selectionne
                    x = indiceSelection2;
                    estSelect = true;
                }
                else
                {
                    indiceSelection2 = -1;
                }

                if (estSelect)
                {
                    // si on doit envoyer qlqchose
                    sf::Packet p2;
                    p2 << eGAME_SELECTION;
                    _client->send(p2, false);
                    p2.clear();
                    p2 << x;
                    _client->send(p2, false);
                }
            }
            else // si aucun territoire selectionne pour le moment
            {
                back->selectionnerTerritoire(sf::Mouse::getPosition().x - mapToGlobal(QPoint(0, 0)).x(), sf::Mouse::getPosition().y - mapToGlobal(QPoint(0, 0)).y(), indiceSelection);
                if ((indiceSelection > -1 && camps[idJoueur].appartient(back->territoires[indiceSelection])) && back->territoires[indiceSelection]->getNbDesCourant() > 1)
                {
                    sf::Packet p2;
                    p2 << eGAME_SELECTION;
                    _client->send(p2, false);
                    p2.clear();
                    p2 << (sf::Int8)indiceSelection;
                    _client->send(p2, false);
                }
                else
                {
                    indiceSelection = -1;
                }
            }
        }
    }

    draw(s);

    if(*etat == eCOMBAT)
    {
        draw(sF);
    }

    if (indiceSelection > -1)
    {
        back->territoires[indiceSelection]->setFillColor(true);
        back->territoires[indiceSelection]->setOutlineColor(true);
        back->territoires[indiceSelection]->dessiner(*this);
    }

    if (indiceSelection2 > -1)
    {
        back->territoires[indiceSelection2]->setFillColor(true);
        back->territoires[indiceSelection2]->setOutlineColor(true);
        back->territoires[indiceSelection2]->dessiner(*this);
    }

    draw(d);

    clock.restart();
}

bool MyCanvas::isIn()
{
    return this->underMouse();
}

bool MyCanvas::isInFinTour()
{
    return ( sf::Mouse::getPosition().x - mapToGlobal(QPoint(0, 0)).x() >= sF.getGlobalBounds().left
          && sf::Mouse::getPosition().x - mapToGlobal(QPoint(0, 0)).x() <= sF.getGlobalBounds().left + sF.getGlobalBounds().width)
          &&(sf::Mouse::getPosition().y - mapToGlobal(QPoint(0, 0)).y() >= sF.getGlobalBounds().top
          && sf::Mouse::getPosition().y - mapToGlobal(QPoint(0, 0)).y() <= sF.getGlobalBounds().top + sF.getGlobalBounds().height);
}
